package com.hx.er.action.base;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.hx.er.bean.base.UserBean;
import com.hx.er.common.logic.UserLoginInfoUtil;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErLanguages;
import com.hx.er.common.system.ErProperties;
import com.hx.er.common.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ErBaseAction extends ActionSupport {
	// 序列化ID
	private static final long serialVersionUID = 4926512300151605899L;

	// 操作成功信息
	public static final String ADD_SUCCESS_MSG = "common.message.add.success";
	public static final String ADD_FAILURE_MSG = "common.message.add.failure";
	public static final String EDIT_SUCCESS_MSG = "common.message.edit.success";
	public static final String EDIT_FAILURE_MSG = "common.message.edit.failure";
	public static final String DELETE_SUCCESS_MSG = "common.message.delete.success";
	public static final String DELETE_FAILURE_MSG = "common.message.delete.failure";
	public static final String REVIVE_SUCCESS_MSG = "common.message.revive.success";
	public static final String REVIVE_FAILURE_MSG = "common.message.revive.failure";
	public static final String OPERATION_SUCCESS_MSG = "common.message.operation.success";
	public static final String OPERATION_FAILURE_MSG = "common.message.operation.failure";

	// 日期格式
	private final String dateFormat = ErProperties.getProperty(Constants.PROPERTY_COMMON_DATETIME_FORMAT);

	protected static final String BACK = "back";
	protected static final String SAVE = "save";
	protected static final String INIT = "init";
	protected static final String INPUT = "input";
	protected static final String SUCCESS = "success";
	protected static final String FAILURE = "failure";
	protected static final String HOME = "home";
	protected static final String UPLOAD = "upload";

	protected String token;

	protected UserBean userInfoLogin;
	protected String serverName;
	protected String servletPath;
	protected long ipLong;

	protected String fromUrl;

	protected HttpServletRequest request;
	protected HttpServletResponse response;

	public ErBaseAction() {
		// 获取请求
		this.request = ServletActionContext.getRequest();
		this.response = ServletActionContext.getResponse();

		// 获取用户登录信息
		this.userInfoLogin = (UserBean) ActionContext.getContext().getSession().get(Constants.SESSION_USER_INFO);
		// 获取当前路径
		this.servletPath = this.request.getServletPath();
		// 设置IP地址
		String ipStr = UserLoginInfoUtil.getClientIP(this.request);
		this.ipLong = UserLoginInfoUtil.convertIpToLong(ipStr);
		
		// 如果用户已经登录，则使用用户默认语言
		if (this.userInfoLogin != null) {
			ActionContext.getContext().setLocale(ErLanguages.getLocale(this.userInfoLogin.getDefLangId()));
		}
	}

	public String doDefault() {
		try {
			return super.doDefault();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ERROR;
	}

	@Override
	public String execute() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		// 定义方法名
		String methodName = null;
		// 获取请求中的变量列表
		Map<String, String[]> params = request.getParameterMap();
		Object[] keys = params.keySet().toArray();
		// 遍历，获取其中的method:*类型参数，理论上只有一个这种参数
		for (Object key : keys) {
			String keyStr = (String) key;
			if (keyStr.startsWith("method:")) {
				// 去掉"method:"，得到真正的方法名
				methodName = keyStr.replaceAll("method:", "");
			}
		}

		if (!StringUtil.isEmpty(methodName)) {
			// 通过反射机制执行该方法
			return (String) this.getClass().getMethod(methodName).invoke(this, new Object[0]);
		} else {
			return SUCCESS;
		}
	}

	public UserBean getUserInfoLogin() {
		return userInfoLogin;
	}

	public String getServletPath() {
		return servletPath;
	}

	public String getServerName() {
		return serverName;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public String getFromUrl() {
		return fromUrl;
	}

	public void setFromUrl(String fromUrl) {
		this.fromUrl = fromUrl;
	}
}
